package vip.xiaonuo.modular.manager;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.exception.enums.ServerExceptionEnum;


/**
 * @author ZQ
 * @version 1.0
 * @Description
 * @date 2021/3/1 15:26
 */
@Slf4j
public class RequestParamsManager {

    /**
     * data参数为空返回 new空对象
     *
     * @param data
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T transformRequestParam(String data, Class<T> clazz) {
        T reqDto = null;
        try {
            if (null == data || "null".equals(data)) {
                reqDto = clazz.newInstance();
            } else {
                reqDto = JSONObject.parseObject(data,clazz);
            }
        } catch (Exception e) {
            log.error("【RequestParamsManager transformRequestParam】 参数转换异常", e.getMessage());
            throw new ServiceException(ServerExceptionEnum.PARAM_SWITCH.getCode(), ServerExceptionEnum.PARAM_SWITCH.getMessage());
        }
        if (null == reqDto) {
            throw new ServiceException(ServerExceptionEnum.PARAM_SWITCH.getCode(), ServerExceptionEnum.PARAM_SWITCH.getMessage());
        }
        return reqDto;
    }
}
